import Map3DControl from '@/utils/gis/Map3DControl.js'

export default function setupCesiumMap() {
  const map = new window.CgsMap({
    target: 'cesiumContainer',
    projection: 'EPSG:102113',
    center: [118.804, 32.864],
    zoom: 11,
    mode: '3D',
    threeFlag: true,
  })
  window.map = map
  window.map3DControl = new Map3DControl(map)
  map.loadConfig()
  map.setStarVisible(true)
  map.setFogVisible(false)

  loadTileLayer() //加载影像

  loadTerrain() //加载地形

  // 指北针
  const navigation = new CesiumNavigation(map.map3D, {})

  // const viewer = window.map3DControl.viewer
  // viewer.screenSpaceEventHandler.setInputAction((movement) => {
  //   const earthPosition = window.map3DControl.viewer.scene.pickPosition(movement.position)
  //   const cartographic = Cesium.Cartographic.fromCartesian(earthPosition)
  //   const wgs = [
  //     (cartographic.longitude * 180) / Math.PI,
  //     (cartographic.latitude * 180) / Math.PI,
  //     cartographic.height,
  //   ]
  //   console.log('笛卡尔坐标:' + earthPosition)
  //   console.log('wgs84坐标:' + wgs)
  // }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}

/** 加载影像 */
function loadTileLayer() {
  const chemicalStore = useChemicalStore()

  //默认全球影像
  const defaultLayerOptions = {
    url: 'http://49.77.124.50:32010/earthview/services/Globe/RasterService/WMTS',
    layer: 'Globe',
    text: '全球影像',
    level: 11,
    visible: true,
    isAddToLayerTree: true,
    tileMatrixLabels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    pId: 'level1_1',
    type: 'wmts',
    proxy: false,
  }
  window.GTileLayer.add({ name: 'qqyx', parentName: 'qqyx', option: defaultLayerOptions })

  const flag = window.app.config.globalProperties.$config.tileLayerUseOnlineService
  //使用在线服务
  if (flag) {
    const layerOptions = {
      url: 'https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/WMTS',
      layer: 'World_Imagery',
      text: '在线世界影像地图',
      level: 19,
      visible: true,
      isAddToLayerTree: true,
      tileMatrixLabels: [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
        25,
      ],
      pId: 'level1_1',
      type: 'wmts',
      proxy: false,
    }

    window.GTileLayer.add({ name: 'qqyx', parentName: 'qqyx', option: layerOptions })
  }
  //使用后台接口返回的服务
  else {
    chemicalStore.qyInfo.yxxxInfoList &&
      chemicalStore.qyInfo.yxxxInfoList.forEach((item: any) => {
        const option = {
          name: item.id,
          parentName: item.text,
          option: {
            url: item.url,
            layer: item.layer,
            text: item.text,
            level: item.level,
            visible: true,
            isAddToLayerTree: true,
            tileMatrixLabels: JSON.parse(item.tileMatrixLabels),
            pId: item.pId,
            type: item.type,
            proxy: false,
          },
        }
        window.GTileLayer.add(option)
      })
  }
}

/** 加载地形 */
function loadTerrain() {
  const flag = window.app.config.globalProperties.$config.terrainUseOnlineService
  //使用在线服务
  if (flag) {
    //设置cesiumToken
    Cesium.Ion.defaultAccessToken =
      window.app.config.globalProperties.$config.cesiumDefaultAccessToken

    //加载地形
    window.map3DControl.viewer.terrainProvider = Cesium.createWorldTerrain({
      requestWaterMask: false, // required for water effects
      requestVertexNormals: true, // required for terrain lighting
    })
  } else {
  }
}
